package pers.jassor_why;

import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.Server;

public class MyRibbonRule extends AbstractLoadBalancerRule {


	private int x = 0;
	private int y = 0;
	
	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig) {}
	
	@Override
	public Server choose(Object key) {
		ILoadBalancer lb = this.getLoadBalancer();
		if(lb == null) return null;
		while(true) {
			if(Thread.interrupted()) return null;
			
			List<Server> servers = lb.getReachableServers();
			
			if(servers.isEmpty()) return null;
			
			if(++y == 3) {
				y = 0;
				x = (x+1) % servers.size();
			}
			
			Server server = servers.get(x);
			
			if(server == null || !server.isAlive()) {
				Thread.yield();
				continue;
			}
			return server;
		}
	}
}




class MyRibbonRule这是我惯用的注释方法你懂的 extends RandomRule{
	
	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig) {
		System.err.println("?????????initWithNiwsConfig???????????????");
		super.initWithNiwsConfig(clientConfig);
	}
	
	@Override
	public Server choose(Object key) {
		System.err.println("????????????????????????");
		return super.choose(key);
	}
	
	@Override
	public Server choose(ILoadBalancer lb, Object key) {
		System.err.println("??///////?????");
		return super.choose(lb, key);
		
	}
}